const userService = require('../../services/userService');
Page({
  data: {
    isAuthReady:false,
    noticeQueue:[]
  },
  play: function (e) {
    var user = wx.getStorageSync('user');
    let backgroundAudioManager = wx.getBackgroundAudioManager();
    // wx.setStorageSync('backgroundAudioManager',backgroundAudioManager);
    // this.setData({
    //   backgroundAudioManager:backgroundAudioManager
    // });
    backgroundAudioManager.title = '接收销售订单通知';
    backgroundAudioManager.epname = '接收销售订单通知';
    backgroundAudioManager.singer = '店促通';
    backgroundAudioManager.coverImgUrl = 'http://sih5.oss-cn-hangzhou.aliyuncs.com/386d8912ab6437d7/7b3c036ce866cec8.jpg';
    // 设置了 src 之后会自动播放
    // 25秒无声
    // backgroundAudioManager.src = 'http://sih5.oss-cn-hangzhou.aliyuncs.com/789977058e02da25/31e4069e85616b74.mp3';
    backgroundAudioManager.src = 'http://sih5.oss-cn-hangzhou.aliyuncs.com/b26649dc9ffe6745/f743bcf3c3c864ba.mp3';
    backgroundAudioManager.play();
    backgroundAudioManager.onPlay(() => {
      console.log("音乐播放开始");
    })
    backgroundAudioManager.onEnded(() => {
      if (this.data.noticeQueue.length > 0) {
        backgroundAudioManager.src = this.data.noticeQueue.shift().voice_url;
        return;
      }
      userService.getUnVoiceNoticeOrderList({openid:user.openid,project_type:4}).then((data) => {
        if(data.code == 1){
          if (data.data.length > 0) {
            this.setData({
              noticeQueue:data.data
            });
            backgroundAudioManager.src = this.data.noticeQueue.shift().voice_url;
          } else {
            backgroundAudioManager.src = 'http://sih5.oss-cn-hangzhou.aliyuncs.com/b26649dc9ffe6745/f743bcf3c3c864ba.mp3';
            backgroundAudioManager.seek(0);
          }
        } else{
          wx.showToast({
            title: data.msg,
            icon:'none'
          });
        }
      }, (error) => {
        wx.showToast({
          title: 'getUserSet' + error.message,
          icon:'none'
        });
      });

    })
  },
  //授权成功后回调
  authSuccess(e){
    this.setData({
      isAuthReady: true
    });
    //老用户在onShow方法刷新
    if(e && e.detail.userType == 3) {
      return;
    } else {
      var user = wx.getStorageSync('user');
      userService.getVoiceStatus({openid:user.openid}).then((data) => {
        if(data.code == 1){
          if(data.data.voice_status == 1) {
            this.play();
          }
        } else{
          wx.showToast({
            title: data.msg,
            icon:'none'
          });
        }
        wx.redirectTo({
          url: '/pages/allianceIndexH5/allianceIndexH5?userType='+e.detail.userType+'&keeper_uid='+user.keeper_uid+'&clerk_openid='+user.clerk_openid+'&clerk_key='+user.clerk_key
        });
      }, (error) => {
        wx.showToast({
          title: 'changeVoiceStatus' + error.message,
          icon:'none'
        });
      });

    }
  },

  onShow: function(){
    var user = wx.getStorageSync('user');
    if(user) {
      this.authSuccess();
    }
  },
  onLoad: function (options) {
    //用户店员身份变更需要重新登录，存在被店主主动删除情况，所以每次首页都需要重新登录
    wx.removeStorageSync('user');
    wx.setNavigationBarTitle({
      title: '店促通'
    });
  }
});
